package main

/*
 * @lc app=leetcode.cn id=4 lang=golang
 *
 * [4] 寻找两个正序数组的中位数
 */

// @lc code=start
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
	ln1 := len(nums1)
	ln2 := len(nums2)
	if ln1 == 0 && ln2 == 0 {
		return 0
	}
	end := (ln1 + ln2) / 2
	p1, p2 := 0, 0
	var num []int
	for start := 0; start <= end; start++ {
		if p1 < ln1 && p2 < ln2 {
			cur := 0
			if nums1[p1] < nums2[p2] {
				cur = nums1[p1]
				p1++
			} else {
				cur = nums2[p2]
				p2++
			}
			if start >= end-1 {
				num = append(num, cur)
			}
		} else if p1 < ln1 {
			if start >= end-1 {
				num = append(num, nums1[p1])
			}
			p1++

		} else if p2 < ln2 {
			if start >= end-1 {
				num = append(num, nums2[p2])
			}
			p2++
		}
	}
	if (ln1+ln2)%2 == 1 {
		return float64(num[len(num)-1])
	}
	if (ln1+ln2)%2 == 0 {
		return float64(num[0]+num[1]) / 2
	}
	return 0
}

// @lc code=end
